package com.cuixichang.minimalism.basal.base;

import com.cuixichang.minimalism.basal.enums.ResponseCoderEnum;

public class Result<D> {
    private String code;
    private String message;
    private D data;

    private Result(Builder<D> builder){
        this.code = builder.code;
        this.message = builder.message;
        this.data = builder.data;
    }

    public static Result error(String code, String message) {
       return Result.builder().message(message).code(code).build();
    }

    public static <D> Result ok(String message,D data) {
        return Result.builder().message(message)
                .code(ResponseCoderEnum.Success.getCode()).data(data).build();
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public D getData() {
        return data;
    }

    private static Builder builder() {
       return new Builder();
    }

   static class Builder<D> {
        private String code;
        private String message;
        private D data;

        public Builder code(String code) {
            this.code = code;
            return this;
        }

        public Builder message(String message) {
            this.message = message;
            return this;
        }

        public Builder data(D data) {
            this.data = data;
            return this;
        }

        public Result build() {
            return new Result(this);
        }
    }
}
